package com.hj.security.receiver;

import com.hj.security.R;
import com.hj.security.engine.GPSInfoProvider;

import android.app.admin.DevicePolicyManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;

public class SmsReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		System.out.println("接收到短信了了了");
		Object[] pdus = (Object[]) intent.getExtras().get("pdus");
		for(Object pdu : pdus){
			SmsMessage smsMessage = SmsMessage.createFromPdu((byte[])pdu);
			String content = smsMessage.getMessageBody();
			String sender = smsMessage.getOriginatingAddress();
			System.out.println(content);
			if(content.equals("#*location*#")){
				abortBroadcast();
				GPSInfoProvider gpsInfoProvider = GPSInfoProvider.getInstance(context);
				String locatin = gpsInfoProvider.getLocation();
				System.out.println("location:"+locatin);
				if(!"".equals(locatin)){
					//发送短信
					SmsManager smsManager = SmsManager.getDefault();
					smsManager.sendTextMessage(sender, null, locatin, null, null);		
				}
			}else if(content.equals("#*lockscreen*#")){
				DevicePolicyManager manager = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
				//重新设置密码，第一个参数就是密码，第二个参数暂时没有用，但要设置为0  
				//manager.resetPassword("123", 0);
				//进行锁屏
				manager.lockNow();
				abortBroadcast();
			}else if(content.equals("#*wipe*#")){
				DevicePolicyManager manager = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
				manager.wipeData(0);
				abortBroadcast();
			}else if(content.equals("#*alarm*#")){
				//这个方法已经调用的prepare这个方法的啦，所以不用自己调用prepare这个方法
				MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.jxmzf);
				mediaPlayer.setVolume(1.0f, 1.0f);
				mediaPlayer.start();
				abortBroadcast();
			}
		}
	}

}
